package com.bawei.service;

import com.bawei.dao.AreaRepository;
import com.bawei.dao.UserRepository;
import com.bawei.entity.Area;
import com.bawei.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaRepository areaRepository;

    @Override
    public List<Area> getAreaList() {
        List<Area> all = areaRepository.findAll();
        List<Area> provinceList = areaRepository.findByPid(1);
        provinceList.forEach(province->{
            List<Area> cityList = new ArrayList<>();
            all.forEach(city->{
                if(city.getPid().intValue()==province.getId().intValue()){
                    cityList.add(city);

                }
            });

            province.setSubAreaList(cityList);
        });

        return provinceList;
    }

    @Override
    public List<Area> getAreaListByPid() {
        return areaRepository.findByPid(1);
    }
}
